<template>
  <div class="app-container">
    <el-col :xs="24">
      <transition :enter-active-class="proxy?.animate.searchAnimate.enter"
                  :leave-active-class="proxy?.animate.searchAnimate.leave">
        <div class="search" v-show="showSearch">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="68px">
            <el-form-item label="名称" prop="name">
              <el-input v-model="queryParams.name" placeholder="请输入名称" clearable style="width: 240px"/>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="handleQuery" icon="Search">搜索</el-button>
            </el-form-item>
          </el-form>
        </div>
      </transition>

      <el-card shadow="hover">
        <template #header>
          <el-col :span="1.5">
            <el-button type="primary" plain @click="handleAdd()" icon="Plus">新增
            </el-button>
          </el-col>
        </template>
        <el-table v-loading="loading" :data="categoryList">
          <el-table-column label="id" align="center" key="id" prop="id"/>
          <el-table-column label="名称" align="center" key="name" prop="name"/>
          <el-table-column label="创建时间" align="center" key="createTime" prop="createTime"/>
          <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width">
            <template #default="scope">
              <el-tooltip content="修改" placement="top">
                <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"></el-button>
              </el-tooltip>
              <el-tooltip content="删除" placement="top">
                <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"></el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
      </el-card>
    </el-col>
  </div>

  <!-- 添加设备品类对话框 -->
  <el-dialog :title="title" v-model="open" width="600px" append-to-body>
    <el-form :model="form" :rules="rules" ref="categoryRef" label-width="80px">
      <el-row>
        <el-col :span="12">
          <el-form-item label="id" prop="id">
            <el-input v-model="form.id" placeholder="请输入品类id" maxlength="11" :disabled="form.id!=undefined"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入品类名称" maxlength="50"/>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup>
import {addUser, delUser, getUser, listUser, updateUser} from "@/api/system/user";
import {addCategory, delCategory, listCategory, updateCategory} from "@/api/equip/categroy";
import {validateStringAndNumber} from "@/utils/vaild-rule";

const {proxy} = getCurrentInstance();
const showSearch = ref(true);
const total = ref(0);
const open = ref(false)
const title = ref(undefined)
const dialogType = ref(undefined)
const data = reactive({
  form: {},
  queryParams: {
    deptId: undefined
  },
  rules: {
    id: [{required: true, message: "品类id不能为空", trigger: "blur"}, {
      validator: validateStringAndNumber,
      trigger: "blur"
    }],
    name: [{required: true, message: "品类名称不能为空", trigger: "blur"}],
  }
})


/** 重置操作表单 */
function reset() {
  form.value = {
    id: undefined,
    name: undefined
  };
  proxy.resetForm("userRef");
};

const {queryParams, form, rules} = toRefs(data);
const loading = ref(false);
const categoryList = ref([]);

/** 新增按钮操作 */
function handleAdd() {
  reset();
  title.value = "新增设备品类"
  open.value = true;
  dialogType.value = 1
};

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
};

/** 查询品类分类列表 */
function getList() {
  loading.value = true;
  listCategory(proxy.addDateRange(queryParams.value)).then(res => {
    loading.value = false;
    categoryList.value = res.rows;
    total.value = res.total;
  })
};

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const {id, name} = {...row}
  form.value.id = id
  form.value.name = name
  open.value = true;
  title.value = "修改设备品类";
  dialogType.value = 2
};

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除品类编号为"' + row.id + '"的数据项？').then(function () {
    return delCategory(row.id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
};

/** 提交按钮 */
function submitForm() {
  proxy.$refs["categoryRef"].validate(valid => {
    if (valid) {
      if (dialogType.value == 1) {
        addCategory(form.value).then(res => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        })
      } else {
        updateCategory(form.value).then(res => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        })
      }
    }
  });
};

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
};
getList();
</script>

<style lang="scss" scoped>
</style>
